
package com.craterhill.java.ems.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.craterhill.java.ems.common.beans.Role;
import com.craterhill.java.ems.common.beans.Staff;
import com.craterhill.java.ems.service.CountryService;
import com.craterhill.java.ems.service.PositionService;
import com.craterhill.java.ems.service.RoleService;
import com.craterhill.java.ems.service.StaffService;

@Controller
public class StaffManagementController {

	private final StaffService staffService;
	private final PositionService positionService;
	private final RoleService roleService;
	private final CountryService countryService;

	private final int DEFAULT_ROLE_INDEX = 0;

	@Autowired
	public StaffManagementController(final StaffService staffService, final PositionService positionService, final RoleService roleService,
			final CountryService countryService) {
		this.staffService = staffService;
		this.positionService = positionService;
		this.roleService = roleService;
		this.countryService = countryService;
	}

	@RequestMapping(value = "/setupStaffManagementForm.ems", method = RequestMethod.GET)
	public String setupForm(final @RequestParam(required = false, defaultValue = "-1") int staffId, final ModelMap modelMap) {
		List<Role> roleList = roleService.getRoles();
		modelMap.addAttribute("roleList", roleList);

		modelMap.addAttribute("staffList", staffService.getStaffList(roleList.get(DEFAULT_ROLE_INDEX).getRoleId()));
		modelMap.addAttribute("positionList", positionService.getPositions());
		modelMap.addAttribute("countryList", countryService.getCountrys());

		if(staffId != -1){
			modelMap.addAttribute("staffDetails", staffService.getStaff(staffId));
		}

		return "manage_staff";
	}

	@RequestMapping(value = "/getAjaxStaffList.ems", method = RequestMethod.GET)
	public void getStaffListsaveOrUpdateStaff(final @RequestParam int roleId, final HttpServletResponse httpServletResponse) {

		List<Staff> staffList = staffService.getStaffList(roleId);

		JSONArray staffListJson = new JSONArray();
		JSONObject staffJsonObj = null;
		for(Staff staff : staffList){
			staffJsonObj = new JSONObject();
			staffJsonObj.put("staffId", staff.getStaffId());
			staffJsonObj.put("userName", staff.getUserName());
			staffJsonObj.put("firstName", staff.getFirstName());
			staffJsonObj.put("middleName", staff.getMiddleName());
			staffJsonObj.put("lastName", staff.getLastName());
			staffListJson.add(staffJsonObj);
		}

		try{
			httpServletResponse.getWriter().write(staffJsonObj.toString());
		} catch(IOException e){
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/getAjaxStaffDetails.ems", method = RequestMethod.GET)
	public String saveOrUpdateStaff(final @RequestParam int staffId, final ModelMap modelMap) {
		modelMap.addAttribute("staff", staffService.getStaff(staffId));
		return "redirect:setupForm.esm";
	}

	@RequestMapping(value = "/saveOrUpdateStaff.ems", method = RequestMethod.POST)
	public String saveOrUpdateStaff(final @ModelAttribute Staff staff) {
		staffService.saveStaff(staff);
		return "redirect:setupStaffManagementForm.ems";
	}

	@ModelAttribute
	public Staff getStaff() {
		return new Staff();
	}
}